package com.intech.android.demo.cms.beans;

import com.intech.android.demo.cms.bo.Category;
import com.intech.android.demo.cms.bo.Group;
import com.intech.android.demo.cms.bo.Region;
import com.intech.android.demo.cms.common.Util;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class ViewingStatisticBean extends BaseBean
{

    private List<Integer> chosenFilterGroups = new ArrayList<Integer>();
    private List<Integer> chosenFilterRegions = new ArrayList<Integer>();
    private List<Integer> chosenFilterCategories = new ArrayList<Integer>();
    private List<SelectItem> groups = new ArrayList<SelectItem>();
    private List<SelectItem> regions = new ArrayList<SelectItem>();
    private List<SelectItem> categories;


    public List<SelectItem> getCategories()
    {
        if (Util.isEmpty(categories))
        {
            categories = new ArrayList<SelectItem>();
            List<Category> allCategories = getCategoryDAO().getAll();
            for (Category category : allCategories)
            {
                SelectItem selectItem = new SelectItem(category.getId(), category.getName());
                categories.add(selectItem);
            }
        }
        return categories;
    }

    public void setCategories(List<SelectItem> categories)
    {
        this.categories = categories;
    }

    public List<Integer> getChosenFilterCategories()
    {
        return chosenFilterCategories;
    }

    public void setChosenFilterCategories(List<String> chosenByUserCategories)
    {
        chosenFilterCategories = new ArrayList<Integer>();
        for (String chosenCategory : chosenByUserCategories)
        {
            chosenFilterCategories.add(Integer.parseInt(chosenCategory));
        }
    }

    public void setRegions(List<SelectItem> regions)
    {
        this.regions = regions;
    }

    public List<SelectItem> getRegions()
    {
        if (Util.isEmpty(regions))
        {
            regions = new ArrayList<SelectItem>();
            List<Region> allRegions = getRegionDAO().getAll();
            for (Region region : allRegions)
            {
                SelectItem selectItem = new SelectItem(region.getId(), region.getName());
                regions.add(selectItem);
            }
        }
        return regions;
    }

    public void setGroups(List<SelectItem> groups)
    {
        this.groups = groups;
    }

    public List<SelectItem> getGroups()
    {
        if (Util.isEmpty(groups))
        {
            groups = new ArrayList<SelectItem>();
            List<Group> allGroups = getGroupDAO().getAll();
            for (Group group : allGroups)
            {
                SelectItem selectItem = new SelectItem(group.getId(), group.getName());
                groups.add(selectItem);
            }
        }
        return groups;
    }

    public void clearAllRegions(ActionEvent event)
    {
        chosenFilterRegions = new ArrayList<Integer>();
    }

    public void checkAllRegions(ActionEvent event)
    {
        chosenFilterRegions = new ArrayList<Integer>();
        for (SelectItem item : regions)
        {
            chosenFilterRegions.add((Integer) item.getValue());
        }
    }

    public void clearAllGroups(ActionEvent event)
    {
        chosenFilterGroups = new ArrayList<Integer>();
    }

    public void checkAllGroups(ActionEvent event)
    {
        chosenFilterGroups = new ArrayList<Integer>();
        for (SelectItem item : groups)
        {
            chosenFilterGroups.add((Integer) item.getValue());
        }
    }

    public void checkAllCategories(ActionEvent event)
    {
        chosenFilterCategories = new ArrayList<Integer>();
        for (SelectItem item : categories)
        {
            chosenFilterCategories.add((Integer) item.getValue());
        }
        int a = chosenFilterCategories.size();
    }

    public void clearAllCategories(ActionEvent event)
    {
        chosenFilterCategories = new ArrayList<Integer>();
    }

    public List<Integer> getChosenFilterGroups()
    {
        return chosenFilterGroups;
    }

    public void setChosenFilterGroups(List<String> chosenFilterGroups)
    {
    }

    public List<Integer> getChosenFilterRegions()
    {
        return chosenFilterRegions;
    }

    public void setChosenFilterRegions(List<String> chosenFilterRegions)
    {
    }
}
